/**
 * @Author ZhangCuirong
 * @Date 2025/9/17 13:53
 * @description:
 */
public class Solution01dot06dot {
    public String compressString(String S) {
        if(S.isEmpty()){
            return S;
        }
        StringBuilder res = new StringBuilder();
        int count = 1;
        for (int i = 0; i < S.length(); i++) {
            if (i > 0) {
                if (S.charAt(i) == S.charAt(i - 1)) {
                    count++;
                } else {
                    res.append(S.charAt(i - 1));
                    res.append(count);
                    count = 1;
                }
            }
        }
        res.append(S.charAt(S.length() - 1));
        res.append(count);
        if (S.length() <= res.length()) {
            return S;
        } else {
            return res.toString();
        }
    }

    public static void main(String[] args) {
        Solution01dot06dot solution = new Solution01dot06dot();
        System.out.println(solution.compressString("aabcccccaaa"));
        System.out.println(solution.compressString("abbccd"));
    }
}
